package demo1;

/**
 * @Description
 */

public class MySingleList {

    static class ListNode{
        public int val;
        public ListNode next;

        public ListNode(int val){
            this.val = val;
        }
    }
    public ListNode head;

    public void crateList(){
        ListNode Node1 = new ListNode(1);
        ListNode Node2 = new ListNode(2);
        ListNode Node3 = new ListNode(3);
        ListNode Node4 = new ListNode(4);
        ListNode Node5 = new ListNode(5);

        Node1.next = Node2;
        Node2.next = Node3;
        Node3.next = Node4;
        Node4.next = Node5;

        this.head = Node1;

    }
    public void show(){
        ListNode cur = head;
    while(cur!=null){
        System.out.println(cur.val+" ");
        cur = cur.next;
    }
    }
    public int size(){
        ListNode cur = head;
        int count = 0;
        while(head!=null){
           count++;
           cur = cur.next;
        }
        return count;
    }
    public boolean contains(int key){//             这里为社么打印5不行
        ListNode cur = head;
        while(cur.val!=0) {//如果是cur!=null的话那么5的next是null，不会进入循环
            if (cur.val == key) {
                return true;
            } else {
                cur = cur.next;
            }
        }
        return  false;
    }
    public void addFirst(int data){
        ListNode Node = new ListNode(data);
        Node.next = head;
        head = Node;

    }
    public void addList(int data){
        ListNode Node = new ListNode(data);
        if(head == null){
            head = Node;
        }else{
            ListNode cur = head;
            while(cur.next!=null){
                cur = cur.next;
            }
            cur.next = Node;
        }
    }


}
